/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.util;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

/**
 * Snippets for handling common activity actions.
 * 
 * @author Martynas
 *
 */
public class ActivityUtils {
    public static void restartFirstActivity(Activity activity) {
        Intent i = activity.getApplicationContext().getPackageManager().getLaunchIntentForPackage(
                activity.getApplicationContext().getPackageName());
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivity(i);
    }

    /**
     * Creates a fragment and adds it to the activity.
     * 
     * @param fragmentManager
     * @param fragmentClass Fragment class
     * @param tag Fragment tag
     * @param bundle Fragment arguments
     * @see FragmentTransaction#add(int, android.app.Fragment, String)
     */
    public static void addFragment(FragmentManager fragmentManager, Class<? extends Fragment> fragmentClass, String tag, Bundle bundle) {
        Fragment fragment = ClassUtils.instantiateClass(fragmentClass);
        
        if (fragment != null) {
            fragment.setArguments(bundle);
            
            fragmentManager.beginTransaction().add(fragment, tag).commit();
        }
    }
}
